#include <lpc23xx.h>
#include "LCD.h"
unsigned int *FullCANFilter;
unsigned  int *StandardFilter; 								//Declare pointers for the acceptence filter tables 
unsigned  int *GroupStdFilter; 		
unsigned  int *IndividualExtFilter;    
unsigned  int *GroupExtFilter;		

void FullCAN (void)__irq;

unsigned int *FullCANBuffer;	 

 
int main(void)
{
unsigned int  flasher;
    
unsigned int delay; //
	/* Reset all GPIO pins to default: primary function */
    PINSEL0 = 0x00000000;
    PINSEL1 = 0x00000000;
    PINSEL2 = 0x00000000;
    PINSEL3 = 0x00000000;
    PINSEL4 = 0x00000000;
    PINSEL5 = 0x00000000;
    PINSEL6 = 0x00000000;
    PINSEL7 = 0x00000000;
    PINSEL8 = 0x00000000;
    PINSEL9 = 0x00000000;
    PINSEL10 = 0x00000000;
    
    IODIR0 = 0x00000000;
    IODIR1 = 0x00000000;
	  IOSET0 = 0x00000000;
    IOSET1 = 0x00000000;
    
    FIO0DIR = 0x00000000;
    FIO1DIR = 0x00000000;
    FIO2DIR = 0x00000000;
    FIO3DIR = 0x00000000;
    FIO4DIR = 0x00000000;
    
    FIO0SET = 0x00000000;
    FIO1SET = 0x00000000;
    FIO2SET = 0x00000000;
    FIO3SET = 0x00000000;
    FIO4SET = 0x00000000;			
    FIO2DIR = 0x000000FF;
    FIO2MASK = 0xFFFFFF00; 		
   //vLCDInit();
  //vLCDPutc('T'); 
  // vLCDPuts(Message);
PINSEL0                 |=  0x5 | 0x2<<8|0x2<<10;
PCONP                   |=  0x1<<13 | 0x1<<14;
CAN2MOD 	            |=  0x00000001;			//Set CAN controller into reset
CAN2BTR 	            = 	0x0B | 0x05<<16 | 0x02<< 20;
CAN2MOD 	            = 	0x00000000;			//Release CAN controller

CAN1MOD 	           |= 	0x00000001;			//Set CAN controller into reset
CAN1BTR 	            = 	0x0B | 0x05<<16 | 0x02<< 20;
			//enable interrupt

CAN_AFMR			=	0x00000003;						//Disable the Acceptance filters to allow setup of the table
FullCANFilter		=   (unsigned int *)0xE0038000;		//Pointer to Standard filter table
*FullCANFilter		=	0x10010802;
FullCANFilter++;
*FullCANFilter		=	0x08040808;                           //Note first entry disabled to keep an even number o entries in the table
FullCANFilter++;
*FullCANFilter		=	0x08100820;
FullCANFilter++;
*FullCANFilter		=	0x08400880;         

CAN_SFF_SA			=	0x00000010;			 			//Set start address of Standard table
CAN_SFF_GRP_SA		=	0x0000010;						//Set start address of Standard group table
CAN_EFF_SA			=	0x00000010;						//Set start address of Extended table
CAN_EFF_GRP_SA		=	0x00000010;						//Set start address of Extended group table
CAN_EOT     		=	0x00000010;						//Set end of table address
FCANIE              =   0x01;	     //
VICVectPriority23   = 	0x00000000;  					//select a priority slot for a given interrupt
VICVectAddr23 	    = 	(unsigned)FullCAN;				//pass the address of the IRQ into the VIC slot
VICIntEnable 	    = 	0x00800000;			
CAN_AFMR			=	0x00000004;						//Enable Acceptance filters
CAN1MOD 			= 	0x00000000;						//Release CAN controller
CAN2TFI1 	 	    =	0x00040000;			//Set DLC to 4 bytes 	


    lcd_init();					   //
	lcd_clear();				  //
	set_cursor(0,0);			   // Agaf=/
    lcd_print("FullCAN  Example");	 //
while(1)
{
    
    for(flasher =1;flasher<0x00000100;flasher = flasher <<1)
    {
      CAN2TID1 		= 		flasher;			//Set address to 2 Standard Frame
      
       if(CAN2SR & 0x00000004)					//See if Tx Buffer 1 is free
        {

          CAN2TDA1		=		flasher;				//Copy A/D result into first four bytes
          CAN2CMR 		= 		0x00000021;			//Transmit the message
        }
      
        for(delay = 0;delay<0xA0000;delay++)				  	       //simple delay loop
        {
          ;
        }
      }
    
}

}
volatile unsigned int offset;
unsigned int counter = 0;
void FullCAN (void)	 __irq 
{
counter = 0;
for(offset = 2;offset < 0x0008000;offset = offset<<1)
    {
             
      if(offset & FCANIC0)
      {
      FullCANBuffer =  (unsigned int*)(0xE003801C + (counter*12));
      while(!(*FullCANBuffer & 0x03000000))                           //check the SEM bits to make sure the buffer is updated
        {
        ;
        }
      *FullCANBuffer = *FullCANBuffer & (~0x03000000);                  //Clear the SEm bits before reading the buffer
      FullCANBuffer = FullCANBuffer + (unsigned int)0x1;
      FIO2CLR               = 0x000000FF;
      FIO2SET               = *FullCANBuffer;
      FCANIC0 = FCANIC0 &(~offset);
      }
    counter++;
    }
    VICVectAddr 			= 	0x00000000;						         //Signal the end of interrupt
}